# calculate tildeZ and tildeY from fitted model
# 
# Author: guochun
###############################################################################
constructResult=function(fittedModel){
	UseMethod("constructResult")
}

#for logGaussianM object
# tildeY is simply sigma2 
constructResult.logGaussianM=function(fittedModel){
	if(length(fittedModel@covr)!=0)
		tildeZ=var(log(as.numeric(attr(fittedModel,"trend")$v)))
	else{
		tildeZ=0
	}
	tildeY=fittedModel@parameters$sigma2@value
	
	#construct result object
	result=c("tildeZ"=tildeZ,"pForZ"=attr(fittedModel,"global_pForH"),
			"tildeY"=tildeY,"pForY"=attr(fittedModel,"global_pForD"))
	attr(result,"fittedModel")=fittedModel
	return(result)
}

#for ThomasM object
# tildeY is calculated from g(0) 
constructResult.thomasM=function(fittedModel){
	
	if(length(fittedModel@covr)!=0)
		tildeZ=var(log(as.numeric(attr(fittedModel,"trend")$v))-fittedModel@parameters$lambda@value)
	else{
		tildeZ=0
	}
	#TODO it is not correct, replaced it with g(0)
	tildeY=fittedModel@parameters$sigma2@value
	
	#construct result object
	result=c("tildeZ"=tildeZ,"pForZ"=attr(fittedModel,"global_pForH"),
			"tildeY"=tildeY,"pForY"=attr(fittedModel,"global_pForD"))
	attr(result,"fittedModel")=fittedModel
	return(result)
}


constructResult.maternM=function(fittedModel){
	if(length(fittedModel@covr)!=0)
		tildeZ=var(log(as.numeric(attr(fittedModel,"trend")$v)))
	else{
		tildeZ=0
	}
	tildeY=fittedModel@parameters$sigma2@value
	
	#construct result object
	result=c("tildeZ"=tildeZ,"pForZ"=attr(fittedModel,"global_pForH"),
			"tildeY"=tildeY,"pForY"=attr(fittedModel,"global_pForD"))
	attr(result,"fittedModel")=fittedModel
	return(result)
}
